package com.example.wangkai.wangkai.model.impl;

import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.wangkai.wangkai.app.MyApplication;
import com.example.wangkai.wangkai.model.IModel;
import com.example.wangkai.wangkai.model.IUserModel;
import com.example.wangkai.wangkai.utils.GlobalConsts;
import com.google.gson.JsonParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hanamingming on 16/3/4.
 */
public class UserModel implements IUserModel {

	private RequestQueue queue;

	public UserModel() {
		queue = Volley.newRequestQueue(MyApplication.getContext());
	}

	@Override
	public void login(final String phone, String password, final AsyncCallback callback) {
		String url= GlobalConsts.URL_LOAD_Login;
		StringRequest request=new StringRequest(StringRequest.Method.POST,url,new Response.Listener<String>() {
			public void onResponse(String response) {
				//解析resp
				try {
					JSONObject jsonobject=new JSONObject(response);
					if (jsonobject.getInt("code")==GlobalConsts.RESPONSE_CODE_SUCCESS){
						JSONObject userObj = jsonobject.getJSONObject("token");
						String token = jsonobject.getString("token");
						//保存token
						MyApplication app=MyApplication.getContext();
						app.saveToken(token);
						callback.onSuccess(response);
					} else {
						callback.onError(jsonobject.getString("message"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		},new Response.ErrorListener() {
			public void onErrorResponse(VolleyError error) {
				callback.onError("登录失败");
				Log.e("error", "response:"+error.getMessage());
			}
		});
		queue.add(request);
	}

	@Override
	public void loginWithoutPwd(String token, final AsyncCallback callback) {
//		String url = GlobalConsts.URL_LOAD_LOGIN_WITHOUT_PWD + "?token=" + token;
//		StringRequest request = new StringRequest(StringRequest.Method.POST, url, new Response.Listener<String>() {
//			public void onResponse(String response) {
//				try {
//					JSONObject jsonObject = new JSONObject(response);
//					if (jsonObject.getInt("code") == GlobalConsts.RESPONSE_CODE_SUCCESS) {
//						JSONObject userObj = jsonObject.getJSONObject("token");
//		                String token = jsonObject.getString("token");
//						MyApplication app = MyApplication.getContext();
//						app.getToken();
//						callback.onSuccess(response);
//					} else {
//						callback.onError(response);
//					}
//				} catch (JSONException e){
//					e.printStackTrace();
//				}
//			}
//		}, new Response.ErrorListener() {
//			public void onErrorResponse(VolleyError error) {
//			}
//		});
//		queue.add(request);
	}
}
